﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.Controllers
{
    public class ScorecardAbandonedFixtureController : Controller
    {
        CricketClubEntities db = new CricketClubEntities();
        
        Fixture fixture = new Fixture();
        ScorecardHeader scorecardHeader = new ScorecardHeader();
        FixtureRepository fixtureRepository = new FixtureRepository();
        ResultTypeRepository resultTypeRepository = new ResultTypeRepository();
        ClubFixturePlayer clubFixturePlayer = new ClubFixturePlayer();
        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();
        ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail();
        ClubOtherFixtureDetailRepository clubOtherFixtureDetailRepository = new ClubOtherFixtureDetailRepository();
        OpponentFixturePlayer opponentFixturePlayer = new OpponentFixturePlayer();
        OpponentFixturePlayerRepository opponentFixturePlayerRepository = new OpponentFixturePlayerRepository();
        OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail();
        OpponentOtherFixtureDetailRepository opponentOtherFixtureDetailRepository = new OpponentOtherFixtureDetailRepository();
        PlayerSeasonStatistic playerSeasonStatistic = new PlayerSeasonStatistic();
        ScorecardHeaderRepository scorecardHeaderRepository = new ScorecardHeaderRepository();
        ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();

        //
        // GET: /ScorecardAbandonedFixture/EnterAbandonedFixtureDetail

        public ActionResult EnterAbandonedFixtureDetail(int fixtureId, int resultId)
        {
            ScorecardAbandonedFixtureDetailViewModel scorecardAbandonedFixtureDetailViewModel = new ScorecardAbandonedFixtureDetailViewModel();
            scorecardAbandonedFixtureDetailViewModel.ScorecardFixture = fixtureRepository.GetFixture(fixtureId);
            scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail = new ScorecardHeader()
                                                                            {
                                                                                fixture_id = fixtureId,
                                                                                result_id = resultId
                                                                            };
            scorecardAbandonedFixtureDetailViewModel.ScorecardResultType = resultTypeRepository.GetResultType(resultId);

            return View(scorecardAbandonedFixtureDetailViewModel);
        }

        //
        // POST: /ScorecardAbandonedFixture/EnterAbandonedFixtureDetail

        [HttpPost]
        public ActionResult EnterAbandonedFixtureDetail(ScorecardAbandonedFixtureDetailViewModel scorecardAbandonedFixtureDetailViewModel)
        {
            if (ModelState.IsValid)
            {
                string errorMessage = scorecardHeader.Validate(scorecardAbandonedFixtureDetailViewModel);

                if (errorMessage == "")
                {
                    ScorecardAbandonedFixtureEnterTeamsViewModel scorecardAbandonedFixtureEnterTeamsViewModel = new ScorecardAbandonedFixtureEnterTeamsViewModel();
                    scorecardAbandonedFixtureEnterTeamsViewModel.ScorecardFixture = fixtureRepository.GetFixture(scorecardAbandonedFixtureDetailViewModel.ScorecardFixture.fixture_id);
                    scorecardAbandonedFixtureEnterTeamsViewModel.ScorecardHeaderDetail = scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail;
                    scorecardAbandonedFixtureEnterTeamsViewModel.ScorecardClubPlayers = clubFixturePlayerRepository.GetClubFixturePlayers(scorecardAbandonedFixtureEnterTeamsViewModel.ScorecardFixture.fixture_id);
                    scorecardAbandonedFixtureEnterTeamsViewModel.ScorecardOpponentPlayers = opponentFixturePlayerRepository.CreateOpponentFixtureTeam(scorecardAbandonedFixtureEnterTeamsViewModel.ScorecardFixture);

                    if ((scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.batted_first) && (scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.batted) && (scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.bowled))
                    {
                        return View("EnterAbandonedFixtureTeamDetailsBatAndBowlBattedFirst", scorecardAbandonedFixtureEnterTeamsViewModel);
                    }
                    if ((!scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.batted_first) && (scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.batted) && (scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.bowled))
                    {
                        return View("EnterAbandonedFixtureTeamDetailsBatAndBowlBattedSecond", scorecardAbandonedFixtureEnterTeamsViewModel);
                    }
                    if ((scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.batted) && (!scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.bowled))
                    {
                        return View("EnterAbandonedFixtureTeamDetailsBattedOnly", scorecardAbandonedFixtureEnterTeamsViewModel);
                    }
                    if ((!scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.batted) && (scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.bowled))
                    {
                        return View("EnterAbandonedFixtureTeamDetailsBowledOnly", scorecardAbandonedFixtureEnterTeamsViewModel);
                    }
                }
                else
                {
                    ScorecardAbandonedFixtureDetailViewModel reInputData = new ScorecardAbandonedFixtureDetailViewModel();
                    reInputData.ScorecardFixture = fixtureRepository.GetFixture(scorecardAbandonedFixtureDetailViewModel.ScorecardFixture.fixture_id);
                    reInputData.ScorecardHeaderDetail = scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail;
                    reInputData.ScorecardResultType = scorecardAbandonedFixtureDetailViewModel.ScorecardResultType;
                    ViewBag.ErrorMessage = errorMessage;

                    return View(reInputData);
                }
            }
            return View();
        }

        //
        //  POST: ScorecardAbandonedFixture/EnterScoresBatAndBowlBattedFirst
        
        [HttpPost]
        public ActionResult EnterScoresBatAndBowlBattedFirst(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                // Parse form data

                ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture = fixtureRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers = clubFixturePlayer.GetClubBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers, "player_id", "Member.InitialAndLastName");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayer.GetOpponentBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers, "op_player_name", "op_player_name");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubOtherDetail = new ClubOtherFixtureDetail() { fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id };
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentOtherDetail = new OpponentOtherFixtureDetail() { fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id };
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubPlayerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen, "player_id", "Member.InitialAndLastName");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentPlayerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen, "op_player_name", "op_player_name");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.DismissalTypeSelectList = new SelectList(db.DismissalTypes, "dismissal_id", "type");

                // validate team details

                List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmen(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen, scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture);
                List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers);
                List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmen(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen, scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture);
                List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers);

                if ((clubBatsmenErrorMessages.Count() == 0) && (clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0))
                {
                    return View("EnterAbandonedFixtureScoresBatAndBowlBattedFirst", scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel);
                }
                else
                {
                    ScorecardAbandonedFixtureReInputTeamsViewModel reInputData = new ScorecardAbandonedFixtureReInputTeamsViewModel();
                    reInputData.ScorecardFixture = fixtureRepository.GetFixture(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id);
                    reInputData.ScorecardHeaderDetail = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail;
                    reInputData.ScorecardClubPlayers = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen;
                    reInputData.ScorecardOpponentPlayers = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen;
                    reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                    reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                    reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                    reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                    
                    return View("ReInputAbandonedFixtureTeamDetailsBatAndBowlBattedFirst", reInputData);
                }
            }
            return View();
        }
 
        //
        //  POST: ScorecardAbandonedFixture/UpdateAbandonedFixtureScoresBatAndBowlBattedFirst

        public ActionResult UpdateAbandonedFixtureScoresBatAndbowlBattedFirst(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBattedFirst(formCollection);

                // Validate Input

                List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmenScores(scorecardData.ClubBatsmen);
                List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlersScores(scorecardData.ClubBowlers);
                List<string> clubOtherDetailsErrorMessages = clubOtherFixtureDetail.ValidateClubOtherDetails(scorecardData.ClubOtherDetail);
                List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmenScores(scorecardData.OpponentBatsmen);
                List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlersScores(scorecardData.OpponentBowlers);
                List<string> opponentOtherDetailsErrorMessages = opponentOtherFixtureDetail.ValidateOpponentOtherDetails(scorecardData.OpponentOtherDetail);
                List<string> scorecardClubErrorMessages = scorecardData.ValidateClubScorecardTotals(scorecardData);
                List<string> scorecardOpponentErrorMessages = scorecardData.ValidateOpponentScorecardTotals(scorecardData);

                if ((clubBatsmenErrorMessages.Count() == 0) && (clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0) && (clubOtherDetailsErrorMessages.Count() == 0) && (opponentOtherDetailsErrorMessages.Count() == 0) && (scorecardClubErrorMessages.Count() == 0) && (scorecardOpponentErrorMessages.Count() == 0))
                {
                    // update

                    clubFixturePlayer.UpdateClubBatsmen(scorecardData.ClubBatsmen);
                    clubFixturePlayer.UpdateClubBowlers(scorecardData.ClubBowlers);
                    opponentFixturePlayer.UpdateOpponentBatsmen(scorecardData.OpponentBatsmen);
                    opponentFixturePlayer.UpdateOpponentBowlers(scorecardData.OpponentBowlers);
                    clubOtherFixtureDetail.UpdateClubOtherDetails(scorecardData.ClubOtherDetail, scorecardData.ClubBatsmen);
                    opponentOtherFixtureDetail.UpdateOpponentOtherDetails(scorecardData.OpponentOtherDetail, scorecardData.OpponentBatsmen);
                    scorecardHeader.UpdateScorecardHeaderDetails(scorecardData.ScorecardHeaderDetail);

                     // create view model

                    ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                    scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                    scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                    scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                    scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
                    scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture.fixture_id);
                    scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                    scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
                    scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
                    scorecard.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecard.ClubBatsmen);
                    scorecard.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardData.ClubOtherDetail);
                    scorecard.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecard.OpponentBatsmen);
                    scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);

                    // update playerstatistics

                    playerSeasonStatistic.UpdatePlayersStatistics(scorecard.ClubBatsmen);

                    return View("DisplayAbandonedFixtureScorecardBatAndBowlBattedFirst", scorecard);
                }
                else
                {

                    ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();
                                        
                    reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                    reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                    reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                    reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                    reInputData.ClubBowlers = scorecardData.ClubBowlers;
                    reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                    reInputData.ClubOtherDetail = scorecardData.ClubOtherDetail;
                    reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                    reInputData.DismissalTypes = db.DismissalTypes.ToList();
                    reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                    reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                    reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                    reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                    reInputData.ClubOtherDetailErrorMessages = clubOtherDetailsErrorMessages;
                    reInputData.OpponentOtherDetailErrorMessages = opponentOtherDetailsErrorMessages;
                    reInputData.ScorecardClubErrorMessages = scorecardClubErrorMessages;
                    reInputData.ScorecardOpponentErrorMessages = scorecardOpponentErrorMessages;
                    reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                    reInputData.TotalClubExtras = clubOtherFixtureDetail.GetTotalExtras(reInputData.ClubOtherDetail);
                    reInputData.TotalClubWicketsLost = clubFixturePlayer.GetTotalWicketsLost(reInputData.ClubBatsmen);
                    reInputData.TotalClubScore = reInputData.TotalClubBatsmenRuns + reInputData.TotalClubExtras;
                    reInputData.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(reInputData.OpponentBatsmen);
                    reInputData.TotalOpponentExtras = opponentOtherFixtureDetail.GetTotalExtras(reInputData.OpponentOtherDetail);
                    reInputData.TotalOpponentWicketsLost = opponentFixturePlayer.GetTotalWicketsLost(reInputData.OpponentBatsmen);
                    reInputData.TotalOpponentScore = reInputData.TotalOpponentBatsmenRuns + reInputData.TotalOpponentExtras;

                    return View("ReInputAbandonedFixtureScoresBatAndBowlBattedFirst", reInputData);
                }
            }

            return View();
        }

        //
        //  POST: ScorecardAbandonedFixture/EnterScoresBatAndBowlBattedSecond

        [HttpPost]
        public ActionResult EnterScoresBatAndBowlBattedSecond(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                // Parse form data

                ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture = fixtureRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers = clubFixturePlayer.GetClubBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers, "player_id", "Member.InitialAndLastName");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayer.GetOpponentBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers, "op_player_name", "op_player_name");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubOtherDetail = new ClubOtherFixtureDetail() { fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id };
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentOtherDetail = new OpponentOtherFixtureDetail() { fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id };
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubPlayerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen, "player_id", "Member.InitialAndLastName");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentPlayerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen, "op_player_name", "op_player_name");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.DismissalTypeSelectList = new SelectList(db.DismissalTypes, "dismissal_id", "type");

                // validate team details

                List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmen(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen, scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture);
                List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers);
                List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmen(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen, scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture);
                List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers);

                if ((clubBatsmenErrorMessages.Count() == 0) && (clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0))
                {
                    return View("EnterAbandonedFixtureScoresBatAndBowlBattedSecond", scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel);
                }
                else
                {
                    ScorecardAbandonedFixtureReInputTeamsViewModel reInputData = new ScorecardAbandonedFixtureReInputTeamsViewModel();
                    reInputData.ScorecardFixture = fixtureRepository.GetFixture(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id);
                    reInputData.ScorecardHeaderDetail = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail;
                    reInputData.ScorecardClubPlayers = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen;
                    reInputData.ScorecardOpponentPlayers = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen;
                    reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                    reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                    reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                    reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;

                    return View("ReInputAbandonedFixtureTeamDetailsBatAndBowlBattedSecond", reInputData);
                }
            }
            return View();
        }

        //
        // POST: ScorecardAbandonedFixture/UpdateAbandonedFixtureScoresBatAndBowlBattedSecond

        [HttpPost]
        public ActionResult UpdateAbandonedFixtureScoresBatAndbowlBattedSecond(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBattedSecond(formCollection);

                // Validate Input

                List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmenScores(scorecardData.ClubBatsmen);
                List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlersScores(scorecardData.ClubBowlers);
                List<string> clubOtherDetailsErrorMessages = clubOtherFixtureDetail.ValidateClubOtherDetails(scorecardData.ClubOtherDetail);
                List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmenScores(scorecardData.OpponentBatsmen);
                List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlersScores(scorecardData.OpponentBowlers);
                List<string> opponentOtherDetailsErrorMessages = opponentOtherFixtureDetail.ValidateOpponentOtherDetails(scorecardData.OpponentOtherDetail);
                List<string> scorecardClubErrorMessages = scorecardData.ValidateClubScorecardTotals(scorecardData);
                List<string> scorecardOpponentErrorMessages = scorecardData.ValidateOpponentScorecardTotals(scorecardData);

                if ((clubBatsmenErrorMessages.Count() == 0) && (clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0) && (clubOtherDetailsErrorMessages.Count() == 0) && (opponentOtherDetailsErrorMessages.Count() == 0) && (scorecardClubErrorMessages.Count() == 0) && (scorecardOpponentErrorMessages.Count() == 0))
                {
                    //update

                    clubFixturePlayer.UpdateClubBatsmen(scorecardData.ClubBatsmen);
                    clubFixturePlayer.UpdateClubBowlers(scorecardData.ClubBowlers);
                    opponentFixturePlayer.UpdateOpponentBatsmen(scorecardData.OpponentBatsmen);
                    opponentFixturePlayer.UpdateOpponentBowlers(scorecardData.OpponentBowlers);
                    clubOtherFixtureDetail.UpdateClubOtherDetails(scorecardData.ClubOtherDetail, scorecardData.ClubBatsmen);
                    opponentOtherFixtureDetail.UpdateOpponentOtherDetails(scorecardData.OpponentOtherDetail, scorecardData.OpponentBatsmen);
                    scorecardHeader.UpdateScorecardHeaderDetails(scorecardData.ScorecardHeaderDetail);

                    //create view model

                    ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                    scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                    scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                    scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                    scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
                    scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture.fixture_id);
                    scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                    scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
                    scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
                    scorecard.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecard.ClubBatsmen);
                    scorecard.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardData.ClubOtherDetail);
                    scorecard.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecard.OpponentBatsmen);
                    scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);

                    // update playerstatistics

                    playerSeasonStatistic.UpdatePlayersStatistics(scorecard.ClubBatsmen);

                    return View("DisplayAbandonedFixtureScorecardBatAndBowlBattedSecond", scorecard);
                }
                else
                {

                    ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                    reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                    reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                    reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                    reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                    reInputData.ClubBowlers = scorecardData.ClubBowlers;
                    reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                    reInputData.ClubOtherDetail = scorecardData.ClubOtherDetail;
                    reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                    reInputData.DismissalTypes = db.DismissalTypes.ToList();
                    reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                    reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                    reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                    reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                    reInputData.ClubOtherDetailErrorMessages = clubOtherDetailsErrorMessages;
                    reInputData.OpponentOtherDetailErrorMessages = opponentOtherDetailsErrorMessages;
                    reInputData.ScorecardClubErrorMessages = scorecardClubErrorMessages;
                    reInputData.ScorecardOpponentErrorMessages = scorecardOpponentErrorMessages;
                    reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                    reInputData.TotalClubExtras = clubOtherFixtureDetail.GetTotalExtras(reInputData.ClubOtherDetail);
                    reInputData.TotalClubWicketsLost = clubFixturePlayer.GetTotalWicketsLost(reInputData.ClubBatsmen);
                    reInputData.TotalClubScore = reInputData.TotalClubBatsmenRuns + reInputData.TotalClubExtras;
                    reInputData.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(reInputData.OpponentBatsmen);
                    reInputData.TotalOpponentExtras = opponentOtherFixtureDetail.GetTotalExtras(reInputData.OpponentOtherDetail);
                    reInputData.TotalOpponentWicketsLost = opponentFixturePlayer.GetTotalWicketsLost(reInputData.OpponentBatsmen);
                    reInputData.TotalOpponentScore = reInputData.TotalOpponentBatsmenRuns + reInputData.TotalOpponentExtras;

                    return View("ReInputAbandonedFixtureScoresBatAndBowlBattedSecond", reInputData);
                }
            }

            return View();
        }

        //
        //  POST: ScorecardAbandonedFixture/EnterScoresBattedOnly

        [HttpPost]
        public ActionResult EnterScoresBattedOnly(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                // Parse form data

                ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture = fixtureRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.ParseFormCollectionBattedOnly(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayer.GetOpponentBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers, "op_player_name", "op_player_name");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubOtherDetail = new ClubOtherFixtureDetail() { fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id };
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentPlayerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen, "op_player_name", "op_player_name");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.DismissalTypeSelectList = new SelectList(db.DismissalTypes, "dismissal_id", "type");

                // validate team details

                List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmen(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen, scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture);
                List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmen(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen, scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture);
                List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers);

                if ((clubBatsmenErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0))
                {
                    return View("EnterAbandonedFixtureScoresBattedOnly", scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel);
                }
                else
                {
                    ScorecardAbandonedFixtureReInputTeamsViewModel reInputData = new ScorecardAbandonedFixtureReInputTeamsViewModel();
                    reInputData.ScorecardFixture = fixtureRepository.GetFixture(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id);
                    reInputData.ScorecardHeaderDetail = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail;
                    reInputData.ScorecardClubPlayers = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen;
                    reInputData.ScorecardOpponentPlayers = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen;
                    reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                    reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                    reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;

                    return View("ReInputAbandonedFixtureTeamDetailsBattedOnly", reInputData);
                }
            }
            return View();
        }

        //
        // POST /ScorecardAbandonedFixture/UpdateAbandonedFixtureScoresBattedOnly

        [HttpPost]
        public ActionResult UpdateAbandonedFixtureScoresBattedOnly(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBattedOnly(formCollection);

                // Validate Input

                List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmenScores(scorecardData.ClubBatsmen);
                List<string> clubOtherDetailsErrorMessages = clubOtherFixtureDetail.ValidateClubOtherDetails(scorecardData.ClubOtherDetail);
                List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlersScores(scorecardData.OpponentBowlers);
                List<string> scorecardClubErrorMessages = scorecardData.ValidateClubScorecardTotals(scorecardData);
                
                if ((clubBatsmenErrorMessages.Count() == 0) && (clubOtherDetailsErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0) && (scorecardClubErrorMessages.Count() == 0))
                {
                    //update

                    clubFixturePlayer.UpdateClubBatsmen(scorecardData.ClubBatsmen);
                    opponentFixturePlayer.UpdateOpponentBatsmen(scorecardData.OpponentBatsmen);
                    opponentFixturePlayer.UpdateOpponentBowlers(scorecardData.OpponentBowlers);
                    clubOtherFixtureDetail.UpdateClubOtherDetails(scorecardData.ClubOtherDetail, scorecardData.ClubBatsmen);
                    scorecardHeader.UpdateScorecardHeaderDetails(scorecardData.ScorecardHeaderDetail);

                    //create view model

                    ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                    scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                    scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                    scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                    scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture.fixture_id);
                    scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                    scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
                    scorecard.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecard.ClubBatsmen);
                    scorecard.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardData.ClubOtherDetail);
                    
                    // update playerstatistics

                    playerSeasonStatistic.UpdatePlayersStatistics(scorecard.ClubBatsmen);

                    return View("DisplayAbandonedFixtureScorecardBattedOnly", scorecard);
                }
                else
                {

                    ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                    reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                    reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                    reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                    reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                    reInputData.ClubBowlers = scorecardData.ClubBowlers;
                    reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                    reInputData.ClubOtherDetail = scorecardData.ClubOtherDetail;
                    reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                    reInputData.DismissalTypes = db.DismissalTypes.ToList();
                    reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                    reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                    reInputData.ClubOtherDetailErrorMessages = clubOtherDetailsErrorMessages;
                    reInputData.ScorecardClubErrorMessages = scorecardClubErrorMessages;
                    reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                    reInputData.TotalClubExtras = clubOtherFixtureDetail.GetTotalExtras(reInputData.ClubOtherDetail);
                    reInputData.TotalClubWicketsLost = clubFixturePlayer.GetTotalWicketsLost(reInputData.ClubBatsmen);
                    reInputData.TotalClubScore = reInputData.TotalClubBatsmenRuns + reInputData.TotalClubExtras;
                    
                    return View("ReInputAbandonedFixtureScoresBattedOnly", reInputData);
                }
            }

            return View();
        }

        //
        //  POST: ScorecardAbandonedFixture/EnterScoresBowledOnly

        [HttpPost]
        public ActionResult EnterScoresBowledOnly(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                // Parse form data

                ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture = fixtureRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.ParseFormCollection(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.ParseFormCollectionBatOnly(formCollection);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers = clubFixturePlayer.GetClubBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers, "player_id", "Member.InitialAndLastName");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayer.GetOpponentBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen);
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers, "op_player_name", "op_player_name");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubOtherDetail = new ClubOtherFixtureDetail() { fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id };
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentOtherDetail = new OpponentOtherFixtureDetail() { fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id };
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubPlayerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen, "player_id", "Member.InitialAndLastName");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentPlayerSelectList = new SelectList(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen, "op_player_name", "op_player_name");
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.DismissalTypeSelectList = new SelectList(db.DismissalTypes, "dismissal_id", "type");

                // validate team details

                List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlers(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers);
                List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmen(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen, scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture);
                
                if ((clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0))
                {
                    return View("EnterAbandonedFixtureScoresBowledOnly", scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel);
                }
                else
                {
                    ScorecardAbandonedFixtureReInputTeamsViewModel reInputData = new ScorecardAbandonedFixtureReInputTeamsViewModel();
                    reInputData.ScorecardFixture = fixtureRepository.GetFixture(scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id);
                    reInputData.ScorecardHeaderDetail = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail;
                    reInputData.ScorecardClubPlayers = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen;
                    reInputData.ScorecardOpponentPlayers = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen;
                    reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                    reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                    
                    return View("ReInputAbandonedFixtureTeamDetailsBowledOnly", reInputData);
                }
            }
            return View();
        }

        //
        // POST: ScorecardAbandonedFixture/UpdateAbandonedFixtureScoresBowledOnly

        [HttpPost]
        public ActionResult UpdateAbandonedFixtureScoresBowledOnly(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBowledOnly(formCollection);

                // Validate Input

                List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlersScores(scorecardData.ClubBowlers);
                List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmenScores(scorecardData.OpponentBatsmen);
                List<string> opponentOtherDetailsErrorMessages = opponentOtherFixtureDetail.ValidateOpponentOtherDetails(scorecardData.OpponentOtherDetail);
                List<string> scorecardOpponentErrorMessages = scorecardData.ValidateOpponentScorecardTotals(scorecardData);

                if ((clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentOtherDetailsErrorMessages.Count() == 0) && (scorecardOpponentErrorMessages.Count() == 0))
                {
                    //update

                    clubFixturePlayer.UpdateClubBatsmen(scorecardData.ClubBatsmen);
                    clubFixturePlayer.UpdateClubBowlers(scorecardData.ClubBowlers);
                    opponentFixturePlayer.UpdateOpponentBatsmen(scorecardData.OpponentBatsmen);
                    opponentOtherFixtureDetail.UpdateOpponentOtherDetails(scorecardData.OpponentOtherDetail, scorecardData.OpponentBatsmen);
                    scorecardHeader.UpdateScorecardHeaderDetails(scorecardData.ScorecardHeaderDetail);

                    //create view model

                    ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                    scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                    scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                    scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                    scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
                    scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                    scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
                    scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);
                    scorecard.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecard.OpponentBatsmen);
                    scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);

                    // update playerstatistics

                    playerSeasonStatistic.UpdatePlayersStatistics(scorecard.ClubBatsmen);

                    return View("DisplayAbandonedFixtureScorecardBowledOnly", scorecard);
                }
                else
                {

                    ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                    reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                    reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                    reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                    reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                    reInputData.ClubBowlers = scorecardData.ClubBowlers;
                    reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                    reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                    reInputData.DismissalTypes = db.DismissalTypes.ToList();
                    reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                    reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                    reInputData.OpponentOtherDetailErrorMessages = opponentOtherDetailsErrorMessages;
                    reInputData.ScorecardOpponentErrorMessages = scorecardOpponentErrorMessages;
                    reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                    reInputData.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(reInputData.OpponentBatsmen);
                    reInputData.TotalOpponentExtras = opponentOtherFixtureDetail.GetTotalExtras(reInputData.OpponentOtherDetail);
                    reInputData.TotalOpponentWicketsLost = opponentFixturePlayer.GetTotalWicketsLost(reInputData.OpponentBatsmen);
                    reInputData.TotalOpponentScore = reInputData.TotalOpponentBatsmenRuns + reInputData.TotalOpponentExtras;

                    return View("ReInputAbandonedFixtureScoresBowledOnly", reInputData);
                }
            }

            return View();
        }
    }
}
